import java.util.Vector;
import java.util.concurrent.CountDownLatch;

public class Demo6 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);//有多少线程就初始化多少
        for (int i = 0; i < 10; i++) {
            int id = i;
            Thread t = new Thread(()->{
                try {
                    //假设是进行下载这样的耗时操作
                    System.out.println("线程启动:" + id);
                    Thread.sleep(3000);
                    System.out.println("线程结束:"+id);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                countDownLatch.countDown();
            });
            t.start();
        }
        //通过 await 等待所有线程调用 countDown
        countDownLatch.await();//阻塞等待,一直到 countDown 调用的次数和构造方法设定的次数一直的时候,await 才会返回
        System.out.println("所有线程运行完毕");

        /*Vector<String> v = null;
        v.add("2");*/
    }
}
